<template>
  <!-- 企业管理 -->
  <div class="list">
    <div class="query">
      <div>
        <el-input
          v-model.trim="searchinpout.input"
          placeholder="请输入企业名称"
          class="custom-input module"
        ></el-input>
        <el-select
          v-model="searchinpout.status"
          placeholder="请选择行业状态"
          style="margin-right: 10px"
        >
          <el-option label="正常" value="1"></el-option>
          <el-option label="被退回" value="2"></el-option>
        </el-select>
        <el-select v-model="searchinpout.type" placeholder="请选择行业类型">
          <el-option
            v-for="(item, index) in type"
            :label="item.name"
            :value="item.name"
          ></el-option>
        </el-select>
        <!-- <el-input v-model.trim="searchinpout.type" placeholder="请输入行业类型" class="custom-input module"></el-input> -->
        <el-button
          type="primary"
          icon="el-icon-search"
          class="search-button module"
          @click="handleSearch(searchinpout)"
          >搜索</el-button
        >
        <el-button
          icon="el-icon-refresh-left"
          @click="reset"
          class="module"
          style="margin: 0 10px 0 10px"
          >重置</el-button
        >
      </div>
      <el-button
        type="primary"
        icon="el-icon-plus"
        class="search-button module"
        v-show="isAdmin"
        @click="addEnterprises"
        >添加</el-button
      >
    </div>
    <!-- 内容区表格-->
    <div>
      <el-table
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column width="40px"></el-table-column>
        <el-table-column prop="ename" label="企业名称"></el-table-column>
        <el-table-column
          prop="realname"
          label="联系人"
          width="96"
        ></el-table-column>
        <el-table-column prop="phone" label="联系电话"></el-table-column>
        <el-table-column prop="type" label="所属行业"></el-table-column>
        <el-table-column prop="createtime" label="注册时间">
          <template #default="scope">
            {{ scope.row.createtime.split("T")[0] }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <el-button style="width: 100px">{{
              scope.row.status === 2
                ? "被退回"
                : scope.row.status === 1
                ? "正常"
                : "未知状态"
            }}</el-button>
          </template>
        </el-table-column>

        <el-table-column prop="operate" label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              @click="navigateToView(scope.row.id)"
              >查看</el-button
            >
            <el-button
              type="danger"
              size="small"
              @click="handleDeleteUser(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="bottom">
      <div
        class="paging"
        style="
          display: flex;
          justify-content: space-between;
          padding-left: 20px;
        "
      >
        <el-pagination
          :current-page="pageInfo.pagenum"
          layout="total"
          :total="pageInfo.total"
        />
        <!-- 页数选择框 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageInfo.pagenum"
          :page-size="pageInfo.pagesize"
          layout="prev, pager, next"
          :total="pageInfo.total"
          :hide-on-single-page="true"
        />
      </div>
    </div>
  </div>
</template>

<script>
import "element-ui/lib/theme-chalk/index.css";
import { Pagination } from "element-ui";
import {
  getlist,
  getSearch,
  handleDelete,
  handleType,
} from "../../api/enterprise/index";

export default {
  components: {
    [Pagination.name]: Pagination,
  },
  data() {
    return {
      input: "",
      resumeStatus: "",
      flag: "",
      type: [],
      // 表格数据
      searchinpout: {},
      tableData: [],
      pageInfo: {
        pagenum: 1,
        pagesize: 5,
        total: 0,
      },
    };
  },
  mounted() {
    let rname = JSON.parse(localStorage.getItem("rname"));
    console.log(rname, "22212");
    if (rname === "老师") {
      this.isAdmin = true;
    } else {
      this.isAdmin = false;
    }
    this.getlistitem();
    handleType().then((res) => {
      // console.log(res.data, 'aschas')
      this.type = res.data;
      console.log(this.type, "sdafhusd");
    });
  },
  methods: {
    // 搜素功能
    handleSearch(e) {
      const SearchObj = {
        ename: e.input,
        type: e.type,
        status: e.status,
      };

      console.log(SearchObj, "1212312123");
      getSearch(SearchObj)
        .then((res) => {
          console.log(res, "asufjgsaioudfhgsoiadf");
          this.tableData = res.data.records;
          this.pageInfo.total = res.data.total;
        })
        .catch((error) => {
          console.error(error);
        });
    },
    // 重置功能
    reset() {
      this.searchinpout = {};

      this.getlistitem();
    },
    getlistitem() {
      getlist(this.pageInfo)
        .then((res) => {
          this.tableData = res.data.records;
          this.pageInfo.total = res.data.total;
        })
        .catch((error) => {
          console.error(error);
        });
    },
    // 添加企业信息
    addEnterprises() {
      this.$router.push("/addEnterprises");
    },
    // 删除企业
    handleDeleteUser(e) {
      this.$confirm("确认删除该企业吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info",
      })
        .then(() => {
          handleDelete(e).then((res) => {
            this.tableData = res.data.records;
            this.pageInfo.total = res.data.total;
            this.$message({
              type: "success",
              message: "删除成功",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消审核",
          });
        });
    },
    navigateToView(e) {
      this.$router.push({ path: "/auditAdd?id=" + e });
    },
    handleSelectionChange(selection) {
      console.log(selection);
    },
    handlePageChange(page) {
      this.currentPage = page;
      console.log(`当前页码：${page}`);
    },
    // 分页总数
    handleSizeChange(val) {
      this.pageInfo.pagesize = val;
      this.getlistitem();
    },
    //分页当前页
    handleCurrentChange(val) {
      this.pageInfo.pagenum = val;
      this.getlistitem();
    },
  },
};
</script>

<style lang="scss" scoped>
.list {
  margin: 24px;

  .query {
    width: 100%;
    height: 74px;
    display: flex;
    justify-content: space-between;
    padding: 24px 30px 0 30px;
  }
}

.module {
  margin: 24px 10px 25px 0px;
}

.custom-input,
.search-button {
  margin: 0 8px;
  /* 设置左右间距为8像素 */
}

.custom-input {
  width: 256px;
  height: 38px;
}

.newadd {
  margin: 0px 10px 0px 0;
}
</style>